package com.dome.mvp.base;

import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.LifecycleObserver;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import java.lang.ref.WeakReference;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.functions.Consumer;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * Time: 6/1/2021 9:16
 * Author: TAO
 * Description:
 */
public class BasePresenter<V extends IBaseView> implements IBasePresenter<V>, LifecycleObserver, Consumer<Disposable> {

    private WeakReference<V> view;

    @Override
    public void attach(V view) {
        this.view = new WeakReference<V>(view);
    }

    @Override
    public void detach() {
        if (view != null) {
            view.clear();
            view = null;
        }
    }

    @Override
    public void onCreate() {

    }

    @Override
    public void onStart() {

    }

    @Override
    public void onResume() {

    }

    @Override
    public void onPause() {

    }

    @Override
    public void onStop() {

    }

    @Override
    public void onDestroy() {

    }



    public boolean isViewAttached() {
        return view != null && view.get() != null;
    }


    public V getView() {
        if (isViewAttached()) {
            return view.get();
        }
        return null;
    }

    @Override
    public void accept(Disposable disposable) throws Throwable {

    }
}
